import { Component, Input, OnInit, ChangeDetectorRef } from '@angular/core';
import * as Enumerable from 'linq';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { ZepridePageTemplate, ZeprideTemplateComponent } from 'src/app/models';
import { PermitItem } from 'src/app/models/permitItem';
import { PermitService } from 'src/app/services/permit.service';

@Component({
  selector: 'zepride-node-permit-modal',
  templateUrl: './node-permit-modal.component.html',
  styleUrls: ['./node-permit-modal.component.less']
})
export class NodePermitModalComponent implements OnInit {
  @Input() data: IViewPageTemplate[];
  @Input() permits: PermitItem[] = [];
  @Input() mode: string;

  selectedTmpItem: IViewPageTemplate = {};
  tmpPermits: PermitItem[] = [];
  searchInput = '';
  sourceData: IViewPageTemplate[];

  leftTemplateList: ILeftTemplate[] = [];
  selectedLeftTemplate: ILeftTemplate;

  // 历史数据修改
  historyEdit;
  // 所有数据查询
  dataQuery;

  get selctedTmpComCount() {
    if (this.selectedTmpItem) {
      return this.selectedTmpItem.template.tmpComItems.filter(x => x.component.perimitConfig && x.component.perimitConfig.length > 0).length;
    }
    else { return 0; }
  }

  get tmpComItems() {
    if (this.selectedLeftTemplate) {
      let result: ZeprideTemplateComponent[] = [];

      this.selectedLeftTemplate.templateIds.forEach(templateId => {
        const template = this.data.find(x => x.templateId == templateId);
        result = result.concat(template.template.tmpComItems);
      });

      return result;

    }
    else { return []; }
  }

  constructor(
    private modal: NzModalRef,
    private chagneRef: ChangeDetectorRef,
    private permitSvr: PermitService
  ) { }

  ngOnInit(): void {
    if (!this.mode) {
      this.initComPermitConfig();
      // this.tmpSelected(this.data[0]);
      this.leftTemplateClick(this.leftTemplateList[0]);
    }
  }

  tmpSelectedAll() {
    this.tmpComItems.forEach(item => {
      item.permitAllChecked = true;
      item.component.perimitConfig.forEach(pItem => {
        pItem.permitSelected = true;
      });
    });
  }

  tmpCancelSelected() {
    this.tmpComItems.forEach(item => {
      item.component.perimitConfig.forEach(pItem => {
        pItem.permitSelected = !pItem.permitSelected;
      });

      item.permitAllChecked = item.component.perimitConfig.every(n => n.permitSelected);
    });
  }

  tmpSelected(item) {
    this.data.forEach(x => ( x as any).selected = false);
    item.selected = true;
    this.selectedTmpItem = item;
  }

  leftTemplateClick(item: ILeftTemplate) {
    this.leftTemplateList.forEach(x => x.selected = false);
    item.selected = true;

    this.selectedLeftTemplate = item;
  }

  comPermitAllChecked(item: ZeprideTemplateComponent) {
    item.component.perimitConfig.map(x => x.permitSelected = ( item as any).permitAllChecked);
  }

  ok() {
    this.modal.close(this.data);
  }
  cancel() {
    this.modal.close();
  }

  initComPermitConfig() {

    this.data.forEach(item => {
      item.noPermits = true;
      item.template.tmpComItems.forEach(comItem => {
        if (!comItem.component.perimitConfig) {
          return;
        }

        comItem.component.perimitConfig.forEach(pItem => {
          item.noPermits = false;
          pItem.refObjectType = 'TmpComItem';
          pItem.refObjectId = comItem.id;
          if (!pItem.permitName) {
            pItem.permitName = pItem.permitId;
          }
        });

        if (comItem.component.selector == 'tabs' && comItem.component.extends.tabItemConfigs) {
          (comItem.component.extends.tabItemConfigs as { templateIds: string[]; title: string; }[]).forEach(tabItemConfig => {
            tabItemConfig.templateIds.forEach(templateId => {
              const template = this.data.find(x => x.templateId == templateId);
              template.tabRefTmpId = item.templateId;
              template.tabName = tabItemConfig.title;
            });
          });
        }

      });
    });

    this.data.filter(x => !x.tabRefTmpId && !x.noPermits).forEach(item => {
      this.leftTemplateList.push({
        name: item.template.name,
        templateIds: [item.templateId]
      });
    });

    const gList = Enumerable.from(this.data.filter(x => x.tabRefTmpId)).groupBy(x => x.tabName).toArray();

    gList.forEach(item => {
      this.leftTemplateList.push({
        name: item.key(),
        templateIds: item.getSource().map(x => x.templateId)
      });
    });


  }

  searchInputChange() {
    if (!this.sourceData) {
      this.sourceData = JSON.parse(JSON.stringify(this.data));
    }

    if (this.searchInput) {
      this.data = this.data.filter(pageTmpItem => {
        if (pageTmpItem.template) {
          const list = pageTmpItem.template.tmpComItems.filter(tmpComItem => {
            return tmpComItem.component.name.indexOf(this.searchInput) > -1;
          });
          pageTmpItem.template.tmpComItems = list;
          return list.length > 0;
        }
        else {
          return false;
        }
      });
    }
    else {
      this.data = this.sourceData;
    }
    this.selectedTmpItem = this.data[0];

  }

}

interface IViewPageTemplate extends ZepridePageTemplate {
  tabRefTmpId?: string;
  tabName?: string;
  noPermits?: boolean;
}

interface ILeftTemplate {
  name: string;
  templateIds: string[];
  selected?: boolean;
}
